package com.kyub.spring.gears.vo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**

 */
public class NavigableCollection extends ArrayList {

      private Integer currentPage;

      private Integer itemsPerPage;

      private Long totalItems;

      private Integer pages;

    public NavigableCollection(Collection c,Integer currentPage, Integer itemsPerPage, Long totalItems) {
        this.currentPage = currentPage;
        this.itemsPerPage = itemsPerPage;
        this.totalItems = totalItems;
        this.pages=  new Long(1+ (this.totalItems / this.itemsPerPage)).intValue()   ;
        this.addAll(c);
    }

    public  NavigableCollection(Collection c, Integer currentPage, Integer itemsPerPage){

        this.currentPage = currentPage;
        this.itemsPerPage = itemsPerPage;
        this.totalItems = new Long(c.size());
        this.pages=  new Long(1+ (this.totalItems / this.itemsPerPage)).intValue()   ;

        if(totalItems > itemsPerPage){
            Iterator k = c.iterator();
            for(int i= 0; i < currentPage*itemsPerPage; i++){
              //  System.out.println("i + \"k.next()\" = " + i + " " + k.next());
                k.next();
            }
             for(int i= 0; i < itemsPerPage; i++){
               if(k.hasNext()){
                 this.add(k.next());
               }

             }

        }   else{
            this.addAll(c);
        }

    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getItemsPerPage() {
        return itemsPerPage;
    }

    public void setItemsPerPage(Integer itemsPerPage) {
        this.itemsPerPage = itemsPerPage;
    }

    public Long getTotalItems() {
        return totalItems;
    }

    public void setTotalItems(Long totalItems) {
        this.totalItems = totalItems;
    }

    public Integer getPages() {
        return pages;
    }

    public void setPages(Integer pages) {
        this.pages = pages;
    }
}
